import { DataSource, DataSourceOptions } from 'typeorm';

import { env } from './settings'

// 这里到处是为了给 app.module.ts 中使用
export let appDataSourceOptions: DataSourceOptions = {
    type: "mysql",
    host: env.DB_HOST,
    port: env.DB_PORT,
    username: env.DB_USER,
    password: env.DB_PASSWORD,
    database: env.DB_DATABASE,
    entities: [__dirname + '/**/*.entity{.ts,.js}'],
    synchronize: false,
    migrations: [__dirname + '/migrations/*{.ts,.js}'],
    subscribers: [__dirname + '/src/subscribers/*{.ts,.js}'],
};

if (env.isTest) {
    appDataSourceOptions = {
        type: "sqlite",
        database: ':memory:',
        synchronize: true,
        entities: [__dirname + '/**/*.entity{.ts,.js}'],
        subscribers: [__dirname + '/src/subscribers/*{.ts,.js}'],
    }
}

if (env.isDevelopment) {
    Object.assign(appDataSourceOptions, {
        logging: ['schema', 'query', 'error', 'warn', 'migration'],
    });
}

const AppDataSource = new DataSource(appDataSourceOptions);
export default AppDataSource;
